package myschool.biz;

import myschool.dao.StudentDao;
import myschool.entity.Page;
import myschool.entity.Student;
import myschool.entity.Subject;
import myschool.util.MybatisUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author oldliu
 * @since 1.0
 */
public class StudentService {
    public static final StudentService ME = new StudentService();

    public int add(Student stu) {
        SqlSession s = MybatisUtil.get();
        try {
            StudentDao mapper = s.getMapper(StudentDao.class);
            int add = mapper.add(stu);

            return add;
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.close(s);
        }
        return 0;
    }

    public int update(Student stu) {
        SqlSession s = MybatisUtil.get();
        try {
            StudentDao mapper = s.getMapper(StudentDao.class);
            int add = mapper.update(stu);

            return add;
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.close(s);
        }
        return 0;
    }


    public Student get(long stuno) {
        SqlSession s = MybatisUtil.get();
        try {
            StudentDao mapper = s.getMapper(StudentDao.class);
            return mapper.get(stuno);

        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.close(s);
        }
        return null;
    }

    public Student get2(long stuno) {
        SqlSession s = MybatisUtil.get();
        try {
            StudentDao mapper = s.getMapper(StudentDao.class);
            return mapper.get2(stuno);

        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.close(s);
        }
        return null;
    }

    public int del(long stuno) {
        SqlSession s = MybatisUtil.get();
        try {
            StudentDao mapper = s.getMapper(StudentDao.class);
            return mapper.del(stuno);

        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.close(s);
        }
        return 0;
    }


    public List<Student> search1(String sname, String tele,
                                 Date enterdateStart, Date enterDateJieshu, int pageNo, int pageSize) {
        SqlSession s = MybatisUtil.get();
        try {
            StudentDao mapper = s.getMapper(StudentDao.class);
            return mapper.search1(sname, tele, enterdateStart, enterDateJieshu, pageNo, pageSize);

        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.close(s);
        }
        return null;
    }

    public int search1Total(String sname, String tele, Date enterdateStart, Date enterDateJieshu) {
        SqlSession s = MybatisUtil.get();
        try {
            StudentDao mapper = s.getMapper(StudentDao.class);
            return mapper.search1Total(sname, tele, enterdateStart, enterDateJieshu);

        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.close(s);
        }
        return 0;
    }

    //分页方法
    public Page<Student> search(String sname, String tele,
                                  Date enterdateStart, Date enterDateJieshu, int pageNo, int pageSize) {
        List<Student> list = search1(sname, tele, enterdateStart, enterDateJieshu, pageNo, pageSize);
        int total = search1Total(sname, tele, enterdateStart, enterDateJieshu);
        Page<Student> p=new Page<>(list,pageNo,total,pageSize);
        return p;
    }
    public Page<Student> search(String sname, String tele,
                                Date enterdateStart, Date enterDateJieshu, int pageNo) {
        List<Student> list = search1(sname, tele, enterdateStart, enterDateJieshu, pageNo, Page.PAGESIZE);
        int total = search1Total(sname, tele, enterdateStart, enterDateJieshu);
        Page<Student> p=new Page<>(list,pageNo,total,Page.PAGESIZE);
        return p;
    }

    public boolean checkPhone(String phone){
        SqlSession s = MybatisUtil.get();
        try {
            StudentDao mapper = s.getMapper(StudentDao.class);
            return mapper.checkPhone(phone)>0;
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.close(s);
        }
        return false;
    }
}
